package com.situ.mall.admin.controller;


import com.situ.mall.admin.entity.Administrator;
import com.situ.mall.admin.service.IAdministratorService;
import com.situ.mall.admin.util.RequestUtil;
import com.situ.mall.base.exception.InfoException;
import com.situ.mall.base.util.Md5Util;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.ResVO;
import com.situ.mall.base.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author darling
 * @since 2023-03-17
 */
@Controller
@RequestMapping(URLUtil.ADMINISTRATOR)
public class AdministratorController {

    @Autowired
    private IAdministratorService administratorService;

    // 管理员主页
    @GetMapping(URLUtil.PAGE)
    public String page() {
        return URLUtil.ADMINISTRATOR_HTML;
    }

    // 修改密码
    // ----页面跳转
    @GetMapping(URLUtil.CHANGE_PASSWORD)
    public String changePwd() {
        return URLUtil.CHANGE_PASSWORD_HTML;
    }

    // 修改密码
    // ----校验旧密码
    @PostMapping(URLUtil.CHANGE_PASSWORD)
    @ResponseBody
    public void verify(String oldPwd, String newPwd) {
        administratorService.verify(oldPwd, newPwd);
    }

    // 上传头像
    @PostMapping(URLUtil.AVATAR)
    @ResponseBody
    public ResVO avatar(MultipartFile file) {
        return administratorService.avatar(file);
    }

    // 保存管理员信息
    @PostMapping(URLUtil.SAVE)
    @ResponseBody
    public boolean save(Administrator administrator) {
        administrator.setCreateTime(LocalDateTime.now());
        administrator.setType(0);
        administrator.setStatus(0);
        administrator.setPassword(Md5Util.md5(administrator.getPassword()));
        Administrator a = administratorService.selectByUsername(administrator.getUsername());
        if (a == null) {
            return administratorService.saveOrUpdate(administrator);
        } else {
            throw new InfoException("用户名已存在,请更换！", 500);
        }
    }

    // 重置密码
    @PostMapping(URLUtil.RESET_PASSWORD)
    @ResponseBody
    public void resetPwd(Integer id) {
        if (Objects.requireNonNull(RequestUtil.getCurrent()).getType() == 1) {
            Administrator administrator = administratorService.getById(id);
            administrator.setPassword(URLUtil.DEFAULT_PASSWORD_MD5);
            administratorService.saveOrUpdate(administrator);
        } else {
            throw new InfoException("对不起，您没有权限", 500);
        }
    }

    // 修改信息
    // ---- 页面跳转
    @GetMapping(URLUtil.ADMIN_EDIT)
    public String edit() {
        return URLUtil.ADMIN_EDIT_HTML;
    }

    // 修改信息
    // ---- 数据回显
    @GetMapping(URLUtil.UID)
    @ResponseBody
    public Administrator edit(@PathVariable("id") Integer id) {
        return administratorService.getById(id);
    }

    // 修改信息
    // ---- 信息修改
    @PostMapping(URLUtil.EDIT)
    @ResponseBody
    public void edit(Administrator administrator) {
        administratorService.saveOrUpdate(administrator);
    }

    // 删除管理员
    @PostMapping(URLUtil.DELETE)
    @ResponseBody
    public void del(Integer id) {
        Administrator administrator = administratorService.getById(id);
        Administrator currentAdmin = RequestUtil.getCurrent();
        if (Objects.requireNonNull(currentAdmin).getType() == 1) {
            if (administrator != null) {
                if (Objects.equals(administrator.getId(), currentAdmin.getId())) {
                    throw new InfoException("不能删除自己！", 500);
                }
                administrator.setStatus(1);
                administratorService.saveOrUpdate(administrator);
            }
        } else {
            throw new InfoException("对不起，您没有权限", 500);
        }
    }

    // 查询所有管理员
    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit, String keyword) {
        return administratorService.doList(page, limit, keyword);
    }


}
